﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PMS.BusinessFacade;
using PMS.EntityManage;
using PMS.Common;
using PMS.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;

public partial class Modules_Personal_TargetOwnerAdd : System.Web.UI.Page
{
    IPerf_Personal_TargetOwnerHandler toHandler = new Perf_Personal_TargetOwnerManage();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!CheckPurviews())
        {
            Response.Redirect("~/Login.aspx");
        }
        if (!IsPostBack)
        {
            dataBind();
        }
    }

    private bool CheckPurviews()
    {
        if (Session["UserPurviews"] != null)
        {
            string strPur = Session["UserPurviews"].ToString();
            if (strPur.Contains("301021") || strPur.Contains("301022"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }

    private void dataBind()
    {
        if (Request["ID"] != null && Request["ID"] != string.Empty)
        {
            int ID = 0;
            int.TryParse(Request["ID"], out ID);

            if (ID != 0)
            {
                Perf_Personal_TargetOwnerQuery query = toHandler.GetPerf_Personal_TargetOwnerQuery(ID);
                if (query != null)
                {
                    txtName.Text = query.Name;
                    txtRemark.Text = query.Remark;
                }
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if ("".Equals(txtName.Text.Trim()))
        {
            Jscript.Alert("名称不能为空", this.Page);
            return;
        }
        if (Request["ID"] != null && Request["ID"] != string.Empty)
        {
            if (isNameExists(txtName.Text.Trim(), int.Parse(Request["ID"])))
            {
                Jscript.Alert("名称已存在", this.Page);
                return;
            }
        }
        else
        {
            if (isNameExists(txtName.Text.Trim()))
            {
                Jscript.Alert("名称已存在", this.Page);
                return;
            }
        }

        Perf_Personal_TargetOwnerQuery query = new Perf_Personal_TargetOwnerQuery();
        if (Request["ID"] != null && Request["ID"] != string.Empty)
        {
            query = toHandler.GetPerf_Personal_TargetOwnerQuery(int.Parse(Request["ID"]));
        }
        else
        {
            query.Status = (int)Enums.PersonalTargetOwnerStatus.NewAdded;
        }
        query.Name = txtName.Text.Trim();
        query.User_ID = int.Parse(Session["CurrentUserID"].ToString());
        query.Remark = txtRemark.Text;
        query.Create_Date = DateTime.Now;

        if (Request["ID"] != null && Request["ID"] != string.Empty)
        {
            if (toHandler.UpdatePerf_Personal_TargetOwner(query))
            {
                Jscript.Alert("更新成功！", this.Page);
                Jscript.ReturnAndClose(this.Page, "true");
            }
        }
        else
        {
            if (toHandler.InsertPerf_Personal_TargetOwner(query))
            {
                Jscript.Alert("添加成功！", this.Page);
                Jscript.ReturnAndClose(this.Page, "true");
            }
        }
    }


    private bool isNameExists(string name)
    {
        bool istrue = false;

        DataTable dt = toHandler.GetPerf_Personal_TargetOwnerList(" Name = @Name", new SqlParameter[] { new SqlParameter("@Name", name) });
        if (dt != null && dt.Rows.Count > 0)
        {
            istrue = true;
        }
        return istrue;
    }

    private bool isNameExists(string name, int toId)
    {
        bool istrue = false;

        DataTable dt = toHandler.GetPerf_Personal_TargetOwnerList("Name=@Name and ID!=@Id", new SqlParameter[] { new SqlParameter("@Name", name), new SqlParameter("@Id", toId.ToString()) });

        if (dt != null && dt.Rows.Count > 0)
        {
            istrue = true;
        }
        return istrue;
    }

}
